package com.winchain.chainbill.tbill.biz.rx;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.base.utils.MoneyUtil;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.MessageSender;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.message.keyword.SmsTBillDctAprvPassToSerFeeKeyword;
import com.shell.governor.modules.message.keyword.SmsTBillDctAprvPassToZYKeyword;
import com.shell.governor.modules.message.keyword.SmsTBillDctPayInsRjctKeyword;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.tbill.base.po.TbDct;
import com.winchain.chainbill.tbill.base.po.TbOpt;
import com.winchain.chainbill.tbill.base.po.TbRec;
import com.winchain.chainbill.tbill.biz.TBill;
import com.winchain.chainbill.tbill.biz.TBillEvent;
import com.winchain.chainbill.trade.service.BillAcctService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TBillDctAprvMsgSubscriber extends OnErrorNotImplementedSubscriber<TBillEvent<TbDct>> {
	@Autowired
	private CusRecService cusRecService;
	@Autowired
	private BillAcctService billAcctService;

	public void onApplicationEvent(TBillEvent<TbDct> t) {
		String eventCode = t.getOperation().getOptCode();
		if ("E004".equals(eventCode)) {
			sendPassMsg(t);
		} else {
			sendRejectMsg(t);
		}
	}

	private void sendRejectMsg(TBillEvent<TbDct> t) {
		TbDct dct = (TbDct) t.getBill().getSource();
		TbOpt opt = t.getOperation();
		String template = "MT09004";
		SmsTBillDctPayInsRjctKeyword keyword = new SmsTBillDctPayInsRjctKeyword();
		keyword.setDctAmt(MoneyUtil.format(dct.getDctAmt()));
		keyword.setDctDate(SysDateUtil.parseDate(dct.getDctDate(), "yyyyMMdd", "yyyy-MM-dd"));

		keyword.setContactorName(CommonDataUtil.getUserName());
		keyword.setRemark(opt.getRemark());
		String senderId = CommonDataUtil.getUserId();
		MessageSender.sendMessageByTmpl(senderId, template, keyword, dct.getSellerId());
	}

	private void sendPassMsg(TBillEvent<TbDct> t) {
		String senderId = CommonDataUtil.getUserId();
		CusRec artisan = this.cusRecService.getArtisan(null);
		TbDct dct = (TbDct) t.getBill().getSource();
		TbRec rec = t.getBill().getRec();

		String sellerNm = dct.getSellerNm();
		String dctAmt = MoneyUtil.format(dct.getDctAmt());
		String dctInt = MoneyUtil.format(dct.getDctInt());
		String serFee = MoneyUtil.format(dct.getSerFee());
		String billCode = rec.getBillCode();
		String template = "MT09002";
		SmsTBillDctAprvPassToZYKeyword smsTBillDctAprvPassToZYKeyword = new SmsTBillDctAprvPassToZYKeyword();
		smsTBillDctAprvPassToZYKeyword.setDctAmt(dctAmt);
		smsTBillDctAprvPassToZYKeyword.setCusName(sellerNm);
		smsTBillDctAprvPassToZYKeyword.setPayerNm(dct.getBuyerNm());
		smsTBillDctAprvPassToZYKeyword.setAcctNo(dct.getBuyerAcct());
		smsTBillDctAprvPassToZYKeyword.setBillCode(billCode);
		smsTBillDctAprvPassToZYKeyword.setSerFee(serFee);
		MessageSender.sendMessageByTmpl(senderId, template, smsTBillDctAprvPassToZYKeyword, artisan.getCusId());
		if (dct.getBuyerId().equals(dct.getSellerId())) {
			template = "MT09003";
		} else {
			template = "MT09010";
		}
		AcctBas acct = this.billAcctService.getAcctBas(artisan.getCusId(), null, "00");
		SmsTBillDctAprvPassToSerFeeKeyword keyword = new SmsTBillDctAprvPassToSerFeeKeyword();
		keyword.setCusName(sellerNm);
		keyword.setDctAmt(dctAmt);
		keyword.setDctInt(dctInt);
		keyword.setSerFee(serFee);
		keyword.setBillCode(billCode);
		keyword.setAcctNo(acct.getAcctNo());
		keyword.setCusName(dct.getSellerNm());
		MessageSender.sendMessageByTmpl(senderId, template, keyword, dct.getBuyerId());
	}
}
